﻿using System;
using System.IO;

namespace BCCServer.Communication
{
    public sealed class MessageProtocol
    {
        public const int HEADLENGTH = 6;

        private byte _Command = 0;
        public byte Command { get => this._Command; }
        private byte _Param = 0;
        public byte Param { get => this._Param; }
        private int _DataLength = 0;
        public int DataLength { get => this._DataLength; }
        private byte[] _MessageData = new byte[0];
        public byte[] MessageData { get => this._MessageData; }
        private byte[] _MoreData = new byte[0];
        public byte[] MoreData { get => this._MoreData; }

        public MessageProtocol(byte[] buffer)
        {
            if (buffer == null || buffer.Length < HEADLENGTH)
            {
                return;
            }
            MemoryStream ms = new MemoryStream(buffer);
            BinaryReader br = new BinaryReader(ms);
            try
            {
                this._Command = br.ReadByte();
                this._Param = br.ReadByte();
                this._DataLength = br.ReadInt32();
                if (buffer.Length - HEADLENGTH >= this._DataLength)
                {
                    this._MessageData = br.ReadBytes(this._DataLength);
                }
                if (buffer.Length - HEADLENGTH - this.DataLength > 0)
                {
                    this._MoreData = br.ReadBytes(buffer.Length - HEADLENGTH - this._DataLength);
                }
            }
            catch (Exception)
            {
                // 异常处理
            }
            br.Close();
            ms.Close();
        }

        public MessageProtocol(byte command, byte param, byte[] messageData)
        {
            this._Command = command;
            this._Param = param;
            this._MessageData = messageData;
            this._DataLength = messageData.Length;
        }

        public byte[] GetBytes()
        {
            try
            {
                byte[] bytes = null;
                MemoryStream ms = new MemoryStream();
                BinaryWriter bw = new BinaryWriter(ms);
                bw.Write(this._Command);
                bw.Write(this._Param);
                bw.Write(this._DataLength);
                bw.Write(this._MessageData);
                bytes = ms.ToArray();
                bw.Close();
                ms.Close();
                return (bytes);
            }
            catch (Exception)
            {
                return (new byte[0]);
            }
        }

        public (byte Command, byte Param, int DataLength, byte[] MessageData) GetMessage()
        {
            (byte Command, byte Param, int DataLength, byte[] MessageData) returnValue = (this._Command, this._Param, this._MessageData.Length, this._MessageData);
            return (returnValue);
        }

        public static (byte Command, byte Param, int DataLength) GetHeadInfo(byte[] buffer)
        {
            (byte Command, byte Param, int DataLength) returnValue = (0, 0, 0);
            if (buffer == null || buffer.Length < HEADLENGTH)
            {
                return (returnValue);
            }
            returnValue.Command = buffer[0];
            returnValue.Param = buffer[1];
            returnValue.DataLength = BitConverter.ToInt32(buffer, 2);
            return (returnValue);
        }
    }
}